package com.alibaba.datax.common.element;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;

import com.alibaba.datax.common.exception.CommonErrorCode;
import com.alibaba.datax.common.exception.DataXException;

/**
 * @类名: CollectionColumn
 * @说明: 集合列
 *
 * @author   leehom
 * @Date	 2022年8月23日 上午9:20:52
 * 修改记录：
 *
 * @see 	 
 */
public class CollectionColumn<T> extends Column {

	public CollectionColumn() {
		this((Collection<T>) null);
	}

	public CollectionColumn(final Collection<T> rawData) {
		// 暂不计算byte size
		super(rawData, Column.Type.COLLECTION, (null == rawData ? 0 : rawData.toString().length()));
	}

	@Override
	public String asString() {
		if (null == this.getRawData()) {
			return null;
		}

		return this.getRawData().toString();
	}

	private void validateDoubleSpecific(final String data) {
		if ("NaN".equals(data) || "Infinity".equals(data)
				|| "-Infinity".equals(data)) {
			throw DataXException.asDataXException(
					CommonErrorCode.CONVERT_NOT_SUPPORT,
					String.format("String[\"%s\"]属于Double特殊类型，不能转为其他类型 .", data));
		}

		return;
	}

	@Override
	public BigInteger asBigInteger() {
		if (null == this.getRawData()) {
			return null;
		}

		this.validateDoubleSpecific((String) this.getRawData());

		try {
			return this.asBigDecimal().toBigInteger();
		} catch (Exception e) {
			throw DataXException.asDataXException(
					CommonErrorCode.CONVERT_NOT_SUPPORT, String.format(
							"String[\"%s\"]不能转为BigInteger .", this.asString()));
		}
	}

	@Override
	public Long asLong() {
		if (null == this.getRawData()) {
			return null;
		}

		this.validateDoubleSpecific((String) this.getRawData());

		try {
			BigInteger integer = this.asBigInteger();
			OverFlowUtil.validateLongNotOverFlow(integer);
			return integer.longValue();
		} catch (Exception e) {
			throw DataXException.asDataXException(
					CommonErrorCode.CONVERT_NOT_SUPPORT,
					String.format("String[\"%s\"]不能转为Long .", this.asString()));
		}
	}

	@Override
	public BigDecimal asBigDecimal() {
		if (null == this.getRawData()) {
			return null;
		}

		this.validateDoubleSpecific((String) this.getRawData());

		try {
			return new BigDecimal(this.asString());
		} catch (Exception e) {
			throw DataXException.asDataXException(
					CommonErrorCode.CONVERT_NOT_SUPPORT, String.format(
							"String [\"%s\"] 不能转为BigDecimal .", this.asString()));
		}
	}

	@Override
	public Double asDouble() {
		throw DataXException.asDataXException(
				CommonErrorCode.CONVERT_NOT_SUPPORT,
				String.format("String[\"%s\"]不能转为Bool .", this.asString()));
	}

	@Override
	public Boolean asBoolean() {
		throw DataXException.asDataXException(
				CommonErrorCode.CONVERT_NOT_SUPPORT,
				String.format("String[\"%s\"]不能转为Bool .", this.asString()));
	}

	@Override
	public Date asDate() {
		throw DataXException.asDataXException(
				CommonErrorCode.CONVERT_NOT_SUPPORT,
				String.format("String[\"%s\"]不能转为Date .", this.asString()));
	}
	
	@Override
	public Date asDate(String dateFormat) {
		throw DataXException.asDataXException(CommonErrorCode.CONVERT_NOT_SUPPORT,
				String.format("String[\"%s\"]不能转为Date .", this.asString()));
	}

	@Override
	public byte[] asBytes() {
		throw DataXException.asDataXException(
				CommonErrorCode.CONVERT_NOT_SUPPORT,
				String.format("String[\"%s\"]不能转为Bytes .", this.asString()));
	}
}
